package com.sh.滑动时间窗口算法;

public class SlidingWindowRateLimiterManualTest {
    public static void main(String[] args) throws InterruptedException {
        SlidingWindowRateLimiter rateLimiter = new SlidingWindowRateLimiter();

        System.out.println("Testing sliding window rate limiter:");
        // 前 100 个请求应该不限流
        for (int i = 0; i < 100; i++) {
            if ( i%10 == 0 ) {
                Thread.sleep(50);
            }
            boolean allowed = rateLimiter.limit();
            System.out.println("请求 " + (i + 1) + " 通过？ " + allowed);
        }

        // 第 101 个请求应该限流
        boolean limited = rateLimiter.limit();
        System.out.println("101st 请求通过？ " + limited);

        try {
            // 等待一段时间，让窗口滑动
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        // 新窗口下第一个请求应该不限流
        boolean newRequestAllowed = rateLimiter.limit();
        System.out.println("New request after sliding allowed: " + newRequestAllowed);
    }
}